package com.flyiu.data.idempotent.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.flyiu.data.idempotent.annotation.Idempotent;
import com.flyiu.data.idempotent.context.IdempotentUtil;
import com.flyiu.data.idempotent.handler.InempotentInvoke;
import com.flyiu.foundational.response.dto.ResponseDTO;

/**
 * 
 * <p>幂等性拦截器
 * </p>
 *
 * @author chenjh@flyiu.com
 * @version $Id: IdempotentAspect.java, v 0.1 2017年9月26日 上午10:51:57 chenjh Exp $
 */
@Order(value = -1)
@Component
@Aspect
public class IdempotentResponseAspect {
    @Autowired
    private InempotentInvoke invoke;

    /**
     * 幂等接口
     */
    @Pointcut("@annotation(com.flyiu.data.idempotent.annotation.Idempotent) || @within(com.flyiu.data.idempotent.annotation.Idempotent) ")
    public void idempotent() {

    }

    @Around("idempotent()")
    public Object doRequest(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Idempotent idempotent = IdempotentUtil.getIdempotentAnnotation(method);
        Object response = joinPoint.proceed();
        invoke.invokeWhileEnd(response, idempotent);
        if (response != null && response instanceof ResponseDTO) {
            ResponseDTO r = (ResponseDTO) response;
            if (r.isSuccess()) {
                
            }
        }
        return response;
    }

}
